import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Future showBottomSheetPicker<T>({
  @required BuildContext context,
  List<SelectItem<T>> items,
  T value,
  ValueChanged<T> onConfirm,
}) async {
  return showModalBottomSheet<T>(
    context: context,
    builder: (context) {
      return BottomSheetPicker<T>(
        items: items,
        onConfirm: onConfirm,
        value: value,
      );
    },
  );
}

class BottomSheetPicker<T> extends StatefulWidget {
  final List<SelectItem> items;
  final ValueChanged<T> onConfirm;
  final T value;

  const BottomSheetPicker({
    Key key,
    @required this.items,
    this.onConfirm,
    this.value,
  }) : super(key: key);

  @override
  _BottomSheetPickerState<T> createState() => _BottomSheetPickerState<T>();
}

class _BottomSheetPickerState<T> extends State<BottomSheetPicker<T>> {
  T currentValue;
  FixedExtentScrollController controller;

  @override
  void initState() {
    super.initState();
    if (widget.items.length > 0 && widget.value == null) {
      currentValue = widget.items[0].value;
      controller = FixedExtentScrollController();
    }

    if (widget.value != null) {
      currentValue = widget.value;
      controller = FixedExtentScrollController(initialItem: widget.items.indexWhere((item) => item.value == widget.value));
    }
  }

  @override
  void dispose() {
    controller?.dispose();
    controller = null;

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: 300,
      child: Column(
        children: <Widget>[
          Container(
            color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                LabelButton(
                  title: '取消',
                  padding: EdgeInsets.all(12),
                  textStyle: TextStyleConstant().blue_28,
                  onTap: () {
                    Navigator.pop(context);
                  },
                ),
                LabelButton(
                  title: '确认',
                  padding: EdgeInsets.symmetric(
                    horizontal: 15.w,
                  ),
                  textStyle: TextStyleConstant().blue_28,
                  onTap: () {
                    if (widget.onConfirm != null && currentValue != null) {
                      widget.onConfirm(currentValue);
                    }
                    Navigator.pop(context, currentValue);
                  },
                ),
              ],
            ),
          ),
          Expanded(
            child: CupertinoPicker(
              backgroundColor: Colors.white,
              scrollController: controller,
              itemExtent: 40,
              children: widget.items
                  .map((item) => Container(
                        height: 40,
                        alignment: Alignment.center,
                        child: Text(
                          item.title,
                          style: TextStyleConstant().normal_1_32,
                        ),
                      ))
                  .toList(),
              onSelectedItemChanged: (index) {
                currentValue = widget.items[index].value;
              },
            ),
          ),
        ],
      ),
    );
  }
}
